package com.itcanteen.medical.patient.controller;

import com.itcanteen.medical.common.annotation.IgnoreResponseAdvice;
import com.itcanteen.medical.common.constant.Constant;
import com.itcanteen.medical.common.exception.AdException;
import com.itcanteen.medical.patient.entity.Patient;
import com.itcanteen.medical.patient.entity.PatientDocument;
import com.itcanteen.medical.patient.service.PatientDocumentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 患者档案 控制层
 * @author 1804A-TeamOne
 * @date 2020-12-17 16:07
 */
@RestController
@RequestMapping("patientDocument")
@CrossOrigin
public class PatientDocumentController {
    @Resource
    private PatientDocumentService patientDocumentService;

    @Resource
    private HttpServletRequest httpServletRequest;

    /**
     * 统一异常 患者档案查询
     * @author 1804A-TeamOne
     * @date 2020-12-17 16:06
     * @return com.itcanteen.medical.patient.entity.PatientDocument
     */
    @GetMapping("select")
    @IgnoreResponseAdvice
    public PatientDocument select () throws AdException {
        //获取session中的患者信息
        Patient patient = (Patient) httpServletRequest.getSession().getAttribute(PatientController.CURRENT_PATIENT);
        if (patient == null) {
            throw new AdException(Constant.ErrorMsg.SESSION_EXPIRATION_ERROR);
        }
        //根据患者id查看个人档案并返回
        return patientDocumentService.findByPatientId(patient.getId());
    }

    /**
     @param patientDocument 患者档案信息
     @return com.itcanteen.medical.patient.entity.PatientDocument
     @throws AdException 统一异常 更新患者档案
     */
    @PutMapping("update")
    public PatientDocument update (@RequestBody PatientDocument patientDocument) throws AdException {
        return patientDocumentService.update(patientDocument);
    }


}